package com.zlm.user.emp.controller;

import com.zlm.common.base.BaseController;
import com.zlm.common.base.PageEntity;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.dto.JsonResultDto;
import com.zlm.common.dto.userEmp.OrgParamsDto;
import com.zlm.common.dto.userEmp.SysOrgDto;
import com.zlm.common.entity.UserTokenDO;
import com.zlm.user.emp.service.SysOrgService;
import com.zlm.user.emp.service.UserTokenService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 企业controller
 * @Author: duyufu
 * @Date: 2019/4/28 14:10
 */
@RestController
@RequestMapping(value = "/org")
public class SysOrgController extends BaseController {

    @Autowired
    private SysOrgService sysOrgService;
    @Autowired
    private UserTokenService userTokenService;

    @GetMapping("/getSysOrg")
    public JsonResultDto getSysOrg(String token,Integer orgId) {
        try {
            SysOrgDto sysOrgDto;
            if (StringUtils.isNotBlank(token)) {
                UserTokenDO loginUser = userTokenService.getLoginUser(token);
                sysOrgDto = sysOrgService.getSysOrg(loginUser.getUserId(),orgId);
            }else {
                sysOrgDto = sysOrgService.getSysOrg(null ,orgId);
            }
            return success(sysOrgDto);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(CodeConstants.CODE_BUSY);
        }
    }

    @PostMapping("/queryStarEnterprise")
    public JsonResultDto queryStarEnterprise(@RequestBody OrgParamsDto orgParamsDto) {
        try {
            PageEntity page = sysOrgService.queryStarEnterprise(orgParamsDto);
            return success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(CodeConstants.CODE_BUSY);
        }
    }
}
